package com.nwpu.coursesupport.service.imple;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.nwpu.coursesupport.criteria.ForumModuleSearchCriteria;
import com.nwpu.coursesupport.entity.ForumModule;
import com.nwpu.coursesupport.entity.Task;
import com.nwpu.coursesupport.entity.bean.JSONResult;
import com.nwpu.coursesupport.mapper.ForumModuleMapper;
import com.nwpu.coursesupport.pagination.PageBounds;
import com.nwpu.coursesupport.service.AbstractService;

@Service
public class ForumService extends AbstractService{
	
	@Autowired
	private ForumModuleMapper forumModuleMapper;
	
	public JSONResult listForum(ForumModuleSearchCriteria fSC){
		JSONResult rst = new JSONResult();
		rst.fail();

		if (fSC == null) {
			return rst.fail("参数有误");
		}

		if (fSC.getPageNo() <= 0) {
			fSC.setPageNo(1);
		}

		int pageSize = 10;
		int totalCount = forumModuleMapper.count(fSC);
		int pageCount = totalCount % pageSize > 0 ? totalCount / pageSize + 1 : totalCount / pageSize;
		if (fSC.getPageNo() > pageCount) {
			fSC.setPageNo(pageCount);
		}
		PageBounds page = new PageBounds();
		page.setNumber(fSC.getPageNo());
		page.setSize(pageSize);

		List<ForumModule> list = forumModuleMapper.find(fSC, page);
		if (list == null) {
			return rst.fail("数据查询失败");
		}

		rst.addData("courseId", fSC.getCourseId());
		rst.addData("list", list);
		rst.addData("pageNo", fSC.getPageNo());
        rst.addData("allpages",pageCount);
		rst.success();
		return rst;
	}

}
